jQuery(document).ready(function() {
	///// SELECT WITH SEARCH /////
	jQuery(".chzn-select").chosen();
	
	jQuery("#employeeSelect").change(function(){
		var id = jQuery("#employeeSelect").val();
		jQuery.ajax({
        	type:"post",
        	dataType:"json",
        	url:"/employee/nameById",
        	data:{id:id},
        	success:function(data) {
        		jQuery("#empName").val(data.name);
        	}
        });
	});
	
	jQuery("#cardName").change(function(){
		var cardName = jQuery("#cardName").val();
		var empId = jQuery("#employeeSelect").val();
		jQuery.ajax({
        	type:"post",
        	dataType:"json",
        	url:"/timecard/calCardMoney",
        	data:{cardName:cardName, empId:empId},
        	success:function(data) {
        		jQuery("#cardMoney").val(data.cardMoney);
        	}
        });
	});
	
	

	/* initialize the calendar */
	jQuery('#calendar').fullCalendar({
		header : {
			left : '',
			center : 'title',
			right : 'today, prev, next'
		},
		
		monthNames : [ "一月", "二月", "三月", "四月",
				"五月", "六月", "七月", "八月", "九月",
				"十月", "十一月", "十二月" ],
		monthNamesShort : [ "一月", "二月", "三月",
				"四月", "五月", "六月", "七月", "八月",
				"九月", "十月", "十一月", "十二月" ],
		dayNames : [ "周日", "周一", "周二", "周三",
				"周四", "周五", "周六" ],
		dayNamesShort : [ "周日", "周一", "周二",
				"周三", "周四", "周五", "周六" ],
		today : [ "今天" ],
		firstDay : 1,
		allDaySlot : false,
		buttonText : {
			prev : '&laquo;',
			next : '&raquo;',
			prevYear : '&nbsp;&lt;&lt;&nbsp;',
			nextYear : '&nbsp;&gt;&gt;&nbsp;',
			today : '今天',
			month : '月',
			week : '周',
			day : '日'
		},
		dayClick: function (date, allDay, jsEvent, view) {//日期点击后弹出的jq ui的框，添加日程记录
			var empId = jQuery("#employeeSelect").val();
			if(empId==null ||empId=="") {
				jQuery.jGrowl("清先在左上角选择员工！！！");
				return;
			}
			var selectdate = jQuery.fullCalendar.formatDate(date, "yyyy-MM-dd");//选择当前日期的时间转换
			var empId = jQuery("#employeeSelect").val();
			jQuery("#empId").val(empId);
			jQuery("#cardDate").val(selectdate);
			var empName = jQuery("#empName").val();
			jQuery("#cardEmpName").val(empName);
			jQuery("#cardName").val("");
			jQuery("#comment").val("");
			jQuery("#comment").removeAttr("disabled");
			jQuery("#add_btn").show();
			jQuery("#edit_btn").hide();
			jQuery("#delete_btn").hide();
			if(view.name == "month") {
				jQuery("#timecard_dialog").dialog({
					width : 500,
					modal : true
				});
			}
        },
        
        eventClick: function(calEvent, jsEvent, view) {
        	//根据id设置
        	jQuery("#add_btn").hide();
			jQuery("#edit_btn").show();
			jQuery("#delete_btn").show();
			var id = calEvent.id;
			jQuery("#cardId").val(id);
			jQuery.ajax({
	        	type:"post",
	        	dataType:"json",
	        	url:"/timecard/info",
	        	data:{cardId:id},
	        	success:function(data) {
	        		var timecard = data.timecard;
	        		var empId = jQuery("#employeeSelect").val();
	    			jQuery("#empId").val(empId);
	        		var empName = jQuery("#empName").val();
	    			jQuery("#cardEmpName").val(empName);
	    			jQuery("#cardName").val(timecard.cardName);
	    			jQuery("#cardMoney").val(timecard.cardMoney);
	    			jQuery("#cardDate").val(timecard.cardDate);
	    			jQuery("#comment").val(timecard.cardComment);
	        	}
	        });
			
			jQuery("#timecard_dialog").dialog({
				width : 500,
				modal : true
			});
            
        }
	});
	
	jQuery.ajax({
    	type:"post",
    	dataType:"json",
    	url:"/cms/order/events",
    	async:true,
    	data:{classroom:id},
    	success:function(data) {
    		var resultCollection = jQuery.parseJSON(data.events);
            jQuery.each(resultCollection, function (index, term) {
            	jQuery("#calendar").fullCalendar('renderEvent', term, true);
            });
    	}
    });
	
	
	jQuery("#timecard_btn").click(function(){
		jQuery("#calendar").fullCalendar('removeEvents');
		var empId = jQuery("#employeeSelect").val();
		jQuery.ajax({
        	type:"post",
        	dataType:"json",
        	url:"/timecard/events",
        	async:true,
        	data:{empId: empId},
        	success:function(data) {
        		if(data.flag) {
        			jQuery.each(data.events, function (index, term) {
                    	jQuery("#calendar").fullCalendar('renderEvent', term, true);
                    });
        		}
                
        	}
        });
	});
	
	jQuery("#timecardForm").submit(function(e){
	    e.preventDefault();
	});
	
	jQuery("#add_btn").click(function(){
		var params = jQuery("#timecardForm").serializeArray(); 
		jQuery.ajax({
			   type: "post",
			   url: "/timecard/add",
			   dataType:"json",
			   data: params,
			   success: function(data){
				   if(data.flag) {
					   jQuery("#calendar").fullCalendar('removeEvents');
					   jQuery.each(data.events, function (index, term) {
		                	jQuery("#calendar").fullCalendar('renderEvent', term, true);
		                });
		                jQuery("#timecard_dialog").dialog("close");
		                jQuery.jGrowl("恭喜您，添加成功！");
				   } else {
					   jQuery.jGrowl("很抱歉，添加失败！");
				   }
			   },
				error:function(e) {  
					jQuery.jGrowl("很抱歉，预订失败！");
		        }
			 });
	});
	
	jQuery("#delete_btn").click(function(){
		var cardId = jQuery("#cardId").val();
		var empId = jQuery("#empId").val();
		jQuery.ajax({
			   type: "post",
			   url: "/timecard/delete",
			   dataType:"json",
			   data: {cardId:cardId, empId:empId},
			   success: function(data){
				   if(data.flag) {
					   jQuery("#calendar").fullCalendar('removeEvents');
					   jQuery.each(data.events, function (index, term) {
		                	jQuery("#calendar").fullCalendar('renderEvent', term, true);
		                });
		                jQuery("#timecard_dialog").dialog("close");
		                jQuery.jGrowl("恭喜您，删除成功！");
				   } else {
					   jQuery.jGrowl("很抱歉，删除失败！");
				   }
			   },
				error:function(e) {  
					jQuery.jGrowl("很抱歉，删除失败！");
		        }
			 });
	});
	
	jQuery("#edit_btn").click(function(){
		var params = jQuery("#timecardForm").serializeArray(); 
		jQuery.ajax({
			   type: "post",
			   url: "/timecard/edit",
			   dataType:"json",
			   data: params,
			   success: function(data){
				   if(data.flag) {
					   jQuery("#calendar").fullCalendar('removeEvents');
					   jQuery.each(data.events, function (index, term) {
		                	jQuery("#calendar").fullCalendar('renderEvent', term, true);
		                });
		                jQuery("#timecard_dialog").dialog("close");
		                jQuery.jGrowl("恭喜您，修改成功！");
				   } else {
					   jQuery.jGrowl("很抱歉，修改失败！");
				   }
			   },
				error:function(e) {  
					jQuery.jGrowl("很抱歉，修改失败！");
		        }
			 });
	});
	
	
	
	// /// SWITCHING LIST FROM 3 COLUMNS TO 2 COLUMN LIST /////
	function reposTitle() {
		if (jQuery(window).width() < 450) {
			if (!jQuery('.fc-header-title').is(':visible')) {
				if (jQuery('h3.calTitle').length == 0) {
					var m = jQuery('.fc-header-title h2')
							.text();
					jQuery(
							'<h3 class="calTitle">' + m
									+ '</h3>').insertBefore(
							'#calendar table.fc-header');
				}
			}
		} else {
			jQuery('h3.calTitle').remove();
		}
	}
	reposTitle();
	
	// /// ON RESIZE WINDOW /////
	jQuery(window).resize(function() {
		reposTitle();
	});
	
	
	
	jQuery("#date").datepicker({
		minDate: new Date()
	});
	
	
	
	jQuery('input:checkbox,input:radio').uniform();
});

